package cn.com.guage.guc.juc;

import java.util.concurrent.CountDownLatch;

//演示 CountDownLatch
public class CountDownLatchDemo {
    //6个同学陆续离开教室之后，班长锁门
    public static void main(String[] args) throws InterruptedException {

        //创建CountDownLatch对象，设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);

        //6个同学陆续离开教室之后
        for (int i = 1; i <=6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+" 号同学在学习");
                System.out.println(Thread.currentThread().getName()+" 号同学在玩耍");             
                System.out.println(Thread.currentThread().getName()+" 号同学离开了教室");
                //计数  -1
                countDownLatch.countDown();

            },String.valueOf(i)).start();
        }

        new Thread(()->{
        	  //等待
            try {
            	//班长线程一直等待，直到CountDownLatch计数器变为0
				countDownLatch.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
             System.out.println(Thread.currentThread().getName()+" 班长锁门走人了");
        },Thread.currentThread().getName()).start();
      
    }
}
